<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>自定义遍历数据</title>
</head>
<body>
  <script>
    /* 
      const banji = {
        name: "终极一班",
        stus: [
          'xiaoming',
          'xiaoning',
          'xiaotian',
          'knight'
        ]
      }

      // 遍历这个对象
      for(let v of banji) {
        console.log(v);
      }

      报错： Uncaught TypeError: banji is not iterable 
    */

    // banji.stus.forEach()

    const banji = {
      name: "终极一班",
      stus: [
        'xiaoming',
        'xiaoning',
        'xiaotian',
        'knight'
      ],
      [Symbol.iterator]() {
        // return {}         // Uncaught TypeError: undefined is not a function
        
        // Iterator result undefined is not an object
        // return {
        //   next: function() {
                            
        //   }
        // }
       
        // 索引变量
        /* 
          let index = -1;
          let _this = this;
          console.log(_this)
          return {
            next: function() {
              if(index < _this.stus.length) {
                ++index;
                return {
                  value: _this.stus[index],
                  done: false
                }
              } else {
                ++index;
                return {
                  value: "完成",
                  done: true
                }
              }
            }
          } 
        */
        
        let index = 0;

        return {
          next: () => {
            if(index < this.stus.length) {
              const result1 = {
                value: this.stus[index],
                done: false
              }
              index++;
              return result1;
            } else {
              return {
                value: undefined,
                done: true
              }
            }
          }
        }
      }
    }

    for(let v of banji) {
      console.log(v);
    }
  </script>
</body>
</html>